package com.gjj.abox.account;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.gjj.abox.BaseActivity;
import com.gjj.abox.account.adapter.CommonAdapter;
import com.gjj.abox.account.viewHolder.CommonViewHolder;
import com.gjj.abox.constant.Constants;
import com.gjj.abox.custom.LoadMoreListView;
import com.gjj.abox.database.R;
import com.gjj.abox.entity.Account;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


public class AccountNameListActivity extends BaseActivity implements OnClickListener {
    protected InputMethodManager manager;
    @Bind(R.id.tv_account_list_title)
    TextView tvAccountListTitle;
    private LoadMoreListView listView;
    private List<Account> dataList;
    private CommonAdapter adapter;
    private String u_name, paltfor;
    public ImageView ivBack, ivAdd;
    private int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accountname);
        ButterKnife.bind(this);
        getParas();
        initView();
        getDataList();
    }

    private void getParas() {
        count = 0;
        paltfor = "";
        u_name = sharedpreferences.getString("u_name", "");
        paltfor = getIntent().getStringExtra(Constants.Platform_Name);
        if(!paltfor.equals("")){
            if(paltfor.length()>5){
                tvAccountListTitle.setText(getString(R.string.account_list)+"("+"<font color='#4194E9'>" + paltfor.substring(0,5) + "</font>"+"...)");
            }else{
                tvAccountListTitle.setText(Html.fromHtml(getString(R.string.account_list)+"("+"<font color='#4194E9'>" + paltfor + "</font>"+")"));
            }
        }
    }

    private void initView() {
        dataList = new ArrayList<>();
        ivBack = (ImageView) findViewById(R.id.iv_account_list_back);
        ivAdd = (ImageView) findViewById(R.id.iv_account_list_add);
        listView = (LoadMoreListView) findViewById(R.id.lmv_account_list);

        listView.setAdapter(adapter = new CommonAdapter<Account>(this, dataList, R.layout.platform_item) {
            @Override
            public void convert(CommonViewHolder helper, Account item) {
                helper.setText(R.id.tv_platform_name, item.getAccountName());
            }
        });

        listView.setOnItemClickListener(new OnItemClickListener() {
            // 设置点击监听事件
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                Intent intent = new Intent(AccountNameListActivity.this, AddAccountActivity.class);
                intent.putExtra(Constants.Edit_Page_Type, Constants.Is_Detail);
                intent.putExtra(Constants.Platform_Name, paltfor);
                intent.putExtra(Constants.UserName, u_name);
                intent.putExtra(Constants.AccountId, dataList.get(arg2).getAccountName());
                startActivity(intent);
            }
        });

        ivAdd.setOnClickListener(this);
        ivBack.setOnClickListener(this);
    }


    private void getDataList() {
        Account account;
        if (boxDb.queryPName(u_name, count) == null || boxDb.queryAccountName(u_name, paltfor) == null) {
            return;
        }
        dataList.clear();
        for (int i = 0; i < (boxDb.queryAccountName(u_name, paltfor)).size(); i++) {
            account = new Account();
            account.setAccountName((boxDb.queryAccountName(u_name, paltfor)).get(i));
            dataList.add(account);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getDataList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_account_list_add:
                Intent intent = new Intent(AccountNameListActivity.this, AddAccountActivity.class);
                intent.putExtra(Constants.Edit_Page_Type, Constants.Is_Add);
                intent.putExtra(Constants.Platform_Name, paltfor);
                intent.putExtra(Constants.UserName, u_name);
                startActivity(intent);
                break;
            case R.id.iv_account_list_back:
                finish();
                break;
        }
    }
}
